home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK2.toast / Development Kits (Disc 2) / ScriptX / Code Samples / untested / tcpip / ifish / animate.sx next >
Encoding:
Text File  |  1996-05-21  |  2.4 KB  |  90 lines  |  [TEXT/ttxt]

  1. --<<<-
  2. -- Filename: 
  3. --     animate.sx
  4.  
  5. -- Other Files Required:
  6. --     This file is loaded by loadme.sx in the animate folder.
  7.  
  8. -- Purpose:  
  9. --    animate.sx contains the class definition for Animation, a scripted class for doing
  10. --    simple animation.
  11.  
  12. -- Specialized Classes:  
  13. --     Animation
  14.  
  15. -- Instructions to User: 
  16. --    Class Animation is a subclass of TwoDShape which uses a clock to iterate through a
  17. --    series of stencils. The series may be any implicit-keyed collection of Stencil subclass
  18. --    instances. At each tick of the clock. the tick method is invoked on the Animation, which
  19. --    changes the boundary to the next stencil in the series. At the end of the series, the
  20. --    Animation automatically loops back to the beginning. If an endAction callback is specified,
  21. --    it is invoked at this time with authorData and self as the arguments.
  22. --
  23. --    EXAMPLE CREATION:
  24. --    new Animation series: #(bm1, bm2, bm3) animationClock: (new Clock scale:10)
  25. --
  26. --    PUBLIC PROTOCOL:
  27. --    start self
  28. --    stop self
  29.  
  30. -- Author:
  31. --     Steve Mayer
  32. in module InternetFish
  33.  
  34. class Animation(TwoDShape)
  35. instance variables
  36.     series
  37.     cell
  38.     animationClock
  39.     authorData
  40.     endAction
  41. end
  42.  
  43. method init self {class Animation} #key series: (#(new Oval x2:100 y2:100)) \
  44.     animationClock: (new Clock scale:5) \
  45.     endAction: (undefined) \
  46.     autoStart: (true) ->
  47. (
  48.     nextMethod self boundary:(series[1]) fill:blackBrush
  49.     self.series := series
  50.     self.cell := 1
  51.     self.animationClock := animationClock
  52.     addPeriodicCallBack self.animationClock (clk -> tick self clk) self.animationClock #() 1
  53.     self.endAction := endAction
  54.     if autoStart do start self
  55.     return self
  56. )
  57.  
  58. method start self {class Animation} ->
  59. (
  60.     self.animationClock.rate := 1
  61. )
  62.  
  63. method stop self {class Animation} ->
  64. (
  65.     self.animationClock.rate := 0
  66. )
  67.  
  68. method seriesEnded self {class Animation} ->
  69. (
  70.     -- Execute endAction callback if there is one.
  71.     if (self.endAction <> undefined) do
  72.         self.endAction self.authorData self
  73. )
  74.  
  75. -- Method tick is called at each tick of the animation clock. It changes
  76. -- the animation cell to the next cell in the series.
  77. method tick self {class Animation} clk ->
  78. (
  79.     -- Change to the appropriate cell
  80.     self.boundary := self.series[self.cell]
  81.     
  82.     -- Get the next cell. Wrap around if at the end.
  83.     self.cell := ((mod self.cell self.series.size) as Integer) + 1
  84.     
  85.     -- If at the end, call seriesEnded.
  86.     if (self.cell = 1) do
  87.         seriesEnded self
  88. )
  89. -- End of class definition for Animation.
  90.